package com.alex.wechat.mp.message.text.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alex.wechat.mp.config.WxContext;
import com.alex.wechat.mp.message.text.entity.WxTextTemplate;
import com.alex.wechat.mp.message.text.service.WxTextTemplateServiceI;

/**
 * 文本消息
 * 
 */
@Controller
@RequestMapping("/wxTextTemplateController")
public class WxTextTemplateController {

  @Autowired
  private WxTextTemplateServiceI textTemplateService;
  @Autowired
  private SystemService systemService;

  /**
   * 转向消息自动回复模板
   * 
   * @return
   */
  @RequestMapping(params = "list")
  public ModelAndView list() {
    return new ModelAndView("com/gomore/wechat/mp/message/text/textTemplateList");
  }

  /**
   * 查询信息列表
   * 
   * @param textTemplate
   * @param request
   * @param response
   * @param dataGrid
   */
  @RequestMapping(params = "datagrid")
  @ResponseBody
  public void datagrid(WxTextTemplate textTemplate, HttpServletRequest request,
      HttpServletResponse response, DataGrid dataGrid) {
    CriteriaQuery cq = new CriteriaQuery(WxTextTemplate.class, dataGrid);
    cq.eq("accountId", WxContext.getOriginalId());
    org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, textTemplate);

    this.textTemplateService.getDataGridReturn(cq, true);
    TagUtil.datagrid(response, dataGrid);
  }

  /**
   * 删除信息
   * 
   * @param textTemplate
   * @param req
   * @return
   */
  @RequestMapping(params = "del")
  @ResponseBody
  public AjaxJson del(WxTextTemplate textTemplate, HttpServletRequest req) {
    AjaxJson j = new AjaxJson();
    textTemplate = this.textTemplateService.getEntity(WxTextTemplate.class, textTemplate.getId());

    this.textTemplateService.delete(textTemplate);

    String message = "删除信息数据成功！";
    systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
    j.setMsg(message);
    return j;
  }

  /**
   * 批量删除文本消息
   * 
   * @return
   */
  @RequestMapping(params = "doBatchDel")
  @ResponseBody
  public AjaxJson doBatchDel(String ids, HttpServletRequest request) {
    AjaxJson j = new AjaxJson();
    String message = "删除信息数据成功";
    int succeed = 0;
    int error = 0;
    try {
      for (String id : ids.split(",")) {
        WxTextTemplate textTemplate = this.textTemplateService.getEntity(WxTextTemplate.class, id);
        this.textTemplateService.delete(textTemplate);
        succeed += 1;
        systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
      }
    } catch (Exception e) {
      e.printStackTrace();
      error += 1;
      message = "删除信息数据失败";
      throw new BusinessException(e.getMessage());
    }
    message = "删除信息数据成功" + succeed + "条，失败" + error + "条";
    j.setMsg(message);
    return j;
  }

  /**
   * 添加或修改消息页面跳转
   * 
   * @param req
   * @return
   */
  @RequestMapping(params = "addorUpdate")
  public ModelAndView addorUpdate(HttpServletRequest req) {
    String id = req.getParameter("id");
    req.setAttribute("id", id);
    if (StringUtil.isNotEmpty(id)) {
      WxTextTemplate textTemplate = this.textTemplateService.getEntity(WxTextTemplate.class, id);
      req.setAttribute("accountId", WxContext.getOriginalId());
      req.setAttribute("templateName", textTemplate.getTemplateName());
      req.setAttribute("content", textTemplate.getContent());
      req.setAttribute("freemarkerSupport", textTemplate.getFreemarkerSupport());
    }
    return new ModelAndView("com/gomore/wechat/mp/message/text/textTemplateInfo");
  }

  /**
   * 保存文本模板修改
   * 
   * @param textTemplate
   * @param req
   * @return
   */
  @RequestMapping(params = "doSave")
  @ResponseBody
  public AjaxJson doSave(WxTextTemplate textTemplate, HttpServletRequest req) {
    AjaxJson j = new AjaxJson();
    String id = textTemplate.getId();
    if (StringUtil.isNotEmpty(id)) {
      WxTextTemplate tempAutoResponse = this.textTemplateService.getEntity(WxTextTemplate.class,
          textTemplate.getId());
      String message = "修改关文本模板成功！";
      try {
        MyBeanUtils.copyBeanNotNull2Bean(textTemplate, tempAutoResponse);
        this.textTemplateService.saveOrUpdate(tempAutoResponse);
        systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
      } catch (Exception e) {
        e.printStackTrace();
      }
    } else {
      if (!WxContext.hasWxAccount()) {
        j.setSuccess(false);
        j.setMsg("请添加一个公众帐号。");
      } else {
        textTemplate.setCreateDate(new Date());
        this.textTemplateService.save(textTemplate);
        j.setSuccess(true);
        j.setMsg("保存成功");
      }
    }
    return j;
  }

}